# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

_realname=libtool
pkgbase=mingw-w64-libtool
pkgname=("${MINGW_PACKAGE_PREFIX}-libtool" "${MINGW_PACKAGE_PREFIX}-libltdl")
pkgver=2.4.7
pkgrel=2
pkgdesc="A generic library support script (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://www.gnu.org/software/libtool"
license=("GPL")
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-cc"
  "automake-wrapper"
  "autoconf-wrapper"
  "help2man"
)
source=("https://ftp.gnu.org/pub/gnu/libtool/${_realname}-${pkgver}.tar.xz"{,.sig}
        0002-cygwin-mingw-Create-UAC-manifest-files.mingw.patch
        0003-Pass-various-runtime-library-flags-to-GCC.mingw.patch
        0005-Fix-seems-to-be-moved.patch
        0006-Fix-strict-ansi-vs-posix.patch
        0007-fix-cr-for-awk-in-configure.all.patch
        0009-libtool-2.4.2.418-msysize.patch
        0010-libtool-2.4.2-include-process-h.patch
        0011-Pick-up-clang_rt-static-archives-compiler-internal-l.patch
        0012-Prefer-response-files-over-linker-scripts-for-mingw-.patch
        0013-Allow-statically-linking-compiler-support-libraries-.patch
        0014-Support-llvm-objdump-f-output.patch
        0015-Allow-statically-linking-Flang-support-libraries.patch)
sha256sums=('4f7f217f057ce655ff22559ad221a0fd8ef84ad1fc5fcb6990cecc333aa1635d'
            'SKIP'
            'fe8b80efd34f9385220ebc90aaec945e44de8c343c75719d6ac0d4e472a6eed5'
            '37d46a7964cf56617b897feb566537d847dee5e8f53e4771255d3cccf722918d'
            'dc39fbe066958178f96108f07db62b48b9339efddf2c21f800ff8d67110ca393'
            '6a94ada08b0a0aa36240efd9ccb826e22ab94ef0969270f2edb8be344dc8c62b'
            'd96beecfc5d15f94ce46bbe0e89d6e6fdb973a25ad6be98c30504b58453792c1'
            '1a858a13592a1a5bd56ae3c4e610408ca19ec9d318ebdfd96bebcfc585b2924f'
            '0f3defa657d353b9f55469f6d514abd96494ce7459ef76bbd63980d8994cafe9'
            'c727b2b017163cfdeca60820d3cff2dac8968c5630745602b150f92b159af313'
            'c95a65e890b1ae6362807abc66809e72cf81aeea5f9f556e38f9752f974bf435'
            '8069e887aeeab7491f15e00547fa66d9b9e86407f5a23f37a6d8c7d165de752e'
            'db16cd322e0ebc578c906e94b0788810af17ce617c700a50db2e3c598dbbed7e'
            '9fe454603a519e9ac50b32de125d4c5293520142c34a895c6e392a190719ff52')
validpgpkeys=('7C5FBB96BE82B954AC20DF5F6EAC957F8EEB55C0') # "Alex Ameen <alex.ameen.tx@gmail.com>"

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/$_patch"
  done
}

prepare() {
  cd "${_realname}-${pkgver}"

  # Note: keep the patches in sync with msys libtool
  apply_patch_with_msg \
    0002-cygwin-mingw-Create-UAC-manifest-files.mingw.patch \
    0003-Pass-various-runtime-library-flags-to-GCC.mingw.patch \
    0005-Fix-seems-to-be-moved.patch \
    0006-Fix-strict-ansi-vs-posix.patch \
    0007-fix-cr-for-awk-in-configure.all.patch \
    \
    0009-libtool-2.4.2.418-msysize.patch \
    0010-libtool-2.4.2-include-process-h.patch \
    0011-Pick-up-clang_rt-static-archives-compiler-internal-l.patch \
    0012-Prefer-response-files-over-linker-scripts-for-mingw-.patch \
    0013-Allow-statically-linking-compiler-support-libraries-.patch \
    0014-Support-llvm-objdump-f-output.patch \
    0015-Allow-statically-linking-Flang-support-libraries.patch
}

build() {
  cd "${_realname}-${pkgver}"

  ./configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST}

  make
}

package_libtool() {
  depends=("${MINGW_PACKAGE_PREFIX}-libltdl")

  cd "${_realname}-${pkgver}"

  make DESTDIR=${pkgdir} install-binSCRIPTS install-man install-info \
                         install-data-local

  # Fix paths
  local WINPREFIX=$(cygpath -wm ${MINGW_PREFIX})
  sed -i "s|${WINPREFIX}|${MINGW_PREFIX}|g" "${pkgdir}${MINGW_PREFIX}/bin/libtool"
}

package_libltdl() {
  pkgdesc="A system independent dlopen wrapper for GNU libtool"

  cd "${_realname}-${pkgver}"

  make DESTDIR=${pkgdir} install-libLTLIBRARIES install-includeHEADERS \
                         install-ltdlincludeHEADERS
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
